package operation;

import book.BookList;
import book.Book;
import java.util.Scanner;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2023-11-16
 * Time: 10:00
 */
public class AddOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增图书的书名：");
        String name = scanner.nextLine();
        System.out.println("请输入你要新增图书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增图书的价格：");
        int price = scanner.nextInt();
        System.out.println("请输入你要新增图书的类型：");
        scanner.nextLine();
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type);


        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)) {
                System.out.println("存在这本书，不能重复添加!");
                return;
            }
        }
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);

    }
}
